#include <stdint.h>

/*
使用SDL_ttf来生成字模
编译
    gcc font_tool.c -shared -o font_tool.dll -lSDL2 -lSDL2_image -lSDL2_ttf -liconv
*/

#include <SDL2/SDL_ttf.h>
#include <iconv.h>
#include <SDL2/SDL_image.h>

TTF_Font*font = 0;

int open_font(const char*path,int size){
    if(!font){
        TTF_Init();
    }
    if(font)TTF_CloseFont(font);
    font = TTF_OpenFont(path,size);
    if(!font)return 0;
    return 1;
}

void _free(void*d){
    if(d){
        free(d);
    }
}

int open_fontmem(void*mem,int memsize,int fontsize){
    if(!font){
        TTF_Init();
    }
    if(font)TTF_CloseFont(font);
    font = TTF_OpenFontRW(
        SDL_RWFromConstMem(mem,memsize),
        1,
        fontsize);
    if(!font)return 0;
    return 1;
}

const char*get_err(){
    return SDL_GetError();
}

int _iconv_open(const char*to,const char*from){
    return (int)iconv_open(to,from);
}

int _iconv_ch_code(int cd,int code){
    char text[BUFSIZ];
    char dest[BUFSIZ];
    char *text_ptr = text;
    char *dest_ptr = dest;
    memset(text,0,sizeof(text));

    int d_len = sizeof(dest);
    *(uint32_t*)dest = 0;
    *(uint32_t*)text = code;
    int s_len = strlen(text);
    iconv( (iconv_t) cd,&text_ptr,&s_len,&dest_ptr,&d_len);
    return *(uint32_t*)dest;
}

static SDL_Surface* getsurface(int ucode,int mode){
    Uint16 text[2];
    text[0] = ((ucode>>8) & 0xff) | (ucode<<8);

    text[1] = 0;
    uint32_t fg = 0xffffffff;
    uint32_t bg = 0x0;
    SDL_Surface *surface;
    if(mode==0){
        surface = TTF_RenderUNICODE(font,text,*(SDL_Color*)&fg,*(SDL_Color*)&bg);
    }
    else if(mode==1){
        surface = TTF_RenderUNICODE_Blended(font,text,*(SDL_Color*)&fg);
    }
    else if(mode==2){
        surface = TTF_RenderUNICODE_Blended_Wrapped(font,text,*(SDL_Color*)&fg,1);
    }
    else if(mode==3){
        surface = TTF_RenderUNICODE_Solid(font,text,*(SDL_Color*)&fg);
    }
    return surface;
}

int   font_save(uint32_t ucode,int mode,const char*path){
    SDL_Surface *surface = getsurface(ucode,mode);
    if(surface){
        int r = IMG_SavePNG(surface,path);
        SDL_FreeSurface(surface);
        return r;
    }
    return 0;
}

SDL_Renderer *renderer = 0;
void* font_read(uint32_t ucode,int mode,int *width,int *height){
    SDL_Surface *surface = getsurface(ucode,mode);
    if(surface){
        int pitch = surface->w * 4;
        uint32_t *pixel = calloc(1,
        pitch*surface->h                        //像素大小
        + (surface->h + 2) * sizeof(void*)      //存放着每行的指针
        + 0x100 //保留
        );
        void **row = (void**)pixel;
        uint32_t *pixel_start = (uint32_t*)((void**)pixel + surface->h + 2);

        #if 0
        --索引格式不支持转换
        int ret = SDL_ConvertPixels(
            surface->w,surface->h,surface->format->format,surface->pixels,surface->pitch,
            SDL_PIXELFORMAT_ABGR8888,pixel_start,pitch
        );
        int i;
        for(i=0;i<surface->h;i++){
            row[i] = (uint8_t*)pixel_start + pitch*i;
        }
        #else
        if(renderer==0){
            SDL_Surface *s = SDL_CreateRGBSurface(0,surface->w,surface->h,32,0xff,0xff00,0xff0000,0xff000000);
            renderer = SDL_CreateSoftwareRenderer(s);
        }
        SDL_SetRenderDrawColor(renderer,0,0,0,0);
        SDL_RenderClear(renderer);

        SDL_Texture *texture = SDL_CreateTextureFromSurface(renderer,surface);
        SDL_RenderCopy(renderer,texture,0,0);
        SDL_RenderPresent(renderer);
        SDL_DestroyTexture(texture);
        SDL_RenderReadPixels(renderer,0,SDL_PIXELFORMAT_ABGR8888,pixel_start,pitch);
        int i;
        for(i=0;i<surface->h;i++){
            row[i] = (uint8_t*)pixel_start + pitch*i;
        }

        #endif


        if(width) *width = surface->w;
        if(height) *height = surface->h;
        SDL_FreeSurface(surface);
        return pixel;
    }
    return 0;
}

